package com.lianjia.analysis.controller.api;

import com.lianjia.analysis.service.CacheService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 缓存API控制器
 * 提供缓存管理相关的API
 */
@RestController
@RequestMapping("/api/cache")
@Tag(name = "缓存API", description = "提供缓存管理相关的API")
public class CacheApiController {

    private static final Logger logger = LoggerFactory.getLogger(CacheApiController.class);

    @Autowired
    @Qualifier("apiCacheService")
    private CacheService cacheService;

    /**
     * 清除指定前缀的缓存
     */
    @DeleteMapping("/clear")
    @Operation(summary = "清除指定前缀的缓存", description = "清除具有特定前缀的所有缓存")
    public ResponseEntity<Map<String, Object>> clearCacheWithPrefix(
            @RequestParam @Parameter(description = "缓存前缀") String prefix) {
        try {
            logger.info("API调用开始: /api/cache/clear, 参数: prefix={}", prefix);
            long startTime = System.currentTimeMillis();
            
            int count = cacheService.clearCacheWithPrefix(prefix);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "已清除前缀为 " + prefix + " 的缓存");
            result.put("count", count);
            
            long endTime = System.currentTimeMillis();
            logger.info("API调用结束: /api/cache/clear, 耗时: {}ms, 清除的缓存数量: {}", (endTime - startTime), count);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("清除缓存异常, 前缀: {}", prefix, e);
            throw e;
        }
    }

    /**
     * 清除所有分析相关的缓存
     */
    @DeleteMapping("/clear-all-analysis")
    @Operation(summary = "清除所有分析相关的缓存", description = "清除所有与分析相关的缓存数据")
    public ResponseEntity<Map<String, Object>> clearAllAnalysisCache() {
        try {
            logger.info("API调用开始: /api/cache/clear-all-analysis");
            long startTime = System.currentTimeMillis();
            
            int count = cacheService.clearCacheWithPrefix("analysis:");
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "已清除所有分析相关的缓存");
            result.put("count", count);
            
            long endTime = System.currentTimeMillis();
            logger.info("API调用结束: /api/cache/clear-all-analysis, 耗时: {}ms, 清除的缓存数量: {}", (endTime - startTime), count);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("清除分析缓存异常", e);
            throw e;
        }
    }

    /**
     * 获取缓存统计信息
     */
    @GetMapping("/stats")
    @Operation(summary = "获取缓存统计信息", description = "返回缓存使用情况的统计信息")
    public ResponseEntity<Map<String, Object>> getCacheStats() {
        try {
            logger.info("API调用开始: /api/cache/stats");
            long startTime = System.currentTimeMillis();
            
            Map<String, Object> stats = cacheService.getCacheStats();
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("cacheEnabled", true);
            result.put("message", "缓存统计信息");
            result.put("stats", stats);
            
            long endTime = System.currentTimeMillis();
            logger.info("API调用结束: /api/cache/stats, 耗时: {}ms", (endTime - startTime));
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            logger.error("获取缓存统计信息异常", e);
            throw e;
        }
    }
} 